import UIKit

class PickerViewExample: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    
    // 选择器视图
    private var pickerView: UIPickerView!
    // 结果标签
    private var resultLabel: UILabel!
    
    // 数据源 - 选项数组
    private let fruits = ["苹果", "香蕉", "橙子", "草莓", "葡萄", "西瓜"]
    private let colors = ["红色", "黄色", "橙色", "粉色", "紫色", "绿色"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        
        setupPickerView()
        setupResultLabel()
    }
    
    private func setupPickerView() {
        pickerView = UIPickerView()
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(pickerView)
        
        NSLayoutConstraint.activate([
            pickerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            pickerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 100),
            pickerView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8),
            pickerView.heightAnchor.constraint(equalToConstant: 200)
        ])
    }
    
    private func setupResultLabel() {
        resultLabel = UILabel()
        resultLabel.text = "当前选择: 苹果 - 红色"
        resultLabel.font = UIFont.systemFont(ofSize: 18)
        resultLabel.textAlignment = .center
        resultLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(resultLabel)
        
        NSLayoutConstraint.activate([
            resultLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            resultLabel.topAnchor.constraint(equalTo: pickerView.bottomAnchor, constant: 40),
            resultLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            resultLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20)
        ])
    }
    
    // MARK: - UIPickerViewDataSource 协议方法
    
    // 返回选择器的列数
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    
    // 返回指定列的行数
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return fruits.count
        case 1:
            return colors.count
        default:
            return 0
        }
    }
    
    // MARK: - UIPickerViewDelegate 协议方法
    
    // 返回每一行的显示内容
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch component {
        case 0:
            return fruits[row]
        case 1:
            return colors[row]
        default:
            return nil
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        switch component {
        case 0:
            let text = fruits[row]
            return NSAttributedString(string: text, attributes: [
                .foregroundColor: UIColor.systemBlue,
            ])
        case 1:
            let text = colors[row]
            
            return NSAttributedString(string: text, attributes: [
                .foregroundColor: UIColor.systemRed,
            ])
        default:
            return nil
        }
    }
    
    // 当用户选择某一行时调用
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let selectedFruit = fruits[pickerView.selectedRow(inComponent: 0)]
        let selectedColor = colors[pickerView.selectedRow(inComponent: 1)]
        
        resultLabel.text = "当前选择: \(selectedFruit) - \(selectedColor)"
    }
    
    // 自定义行高
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 44
    }
    
    // 自定义行视图（可选）
//    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
//        let label = UILabel()
//        label.textAlignment = .center
//        label.font = UIFont.systemFont(ofSize: 18)
//        
//        switch component {
//        case 0:
//            label.text = fruits[row]
//            label.textColor = .systemBlue
//        case 1:
//            label.text = colors[row]
//            label.textColor = .systemRed
//        default:
//            break
//        }
//        
//        return label
//    }
}
